package com.yoso.controller.excel;

import cn.hutool.core.io.IoUtil;
import com.yoso.domain.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shizejin
 * @date 2022/1/27
 * @description:
 */
@RestController
@RequestMapping("excel")
public class MyExcel {
    @RequestMapping("/export")
    public void export(HttpServletResponse response){
        List<User> list = new ArrayList<>();
        User user = new User();
        user.setName("yoso");
        user.setAge(10);
        list.add(user);

        //response为HttpServletResponse对象
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        String name = "test";
        response.setHeader("Content-Disposition","attachment;filename="+name+".xls");
        ServletOutputStream out= null;

        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }
}
